package com.project.slsx.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.adapter.StorageInterfaceAdapter;
import com.project.slsx.bean.AddStorageBean;
import com.project.slsx.bean.ResultBean;
import com.project.slsx.bean.StorageBean;
import com.project.slsx.utils.ParamsUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 入库界面
 */

public class StorageInterfaceActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = StorageInterfaceActivity.class.getSimpleName();
    private StorageInterfaceAdapter adapter;
    private TextView number;

    @Override
    public int getLayoutId() {
        return R.layout.activity_storage_interface;
    }

    @Override
    public void initView() {
        TextView title = findViewById(R.id.tv_title);
        title.setText("入库界面");
        findViewById(R.id.rl_back).setOnClickListener(v -> finish());
        number = findViewById(R.id.tv_storage_number);

        RelativeLayout rlAdd = findViewById(R.id.rl_storage_add_list);
        rlAdd.setOnClickListener(this);
        RecyclerView rv = findViewById(R.id.rv_storeage_interface);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new StorageInterfaceAdapter(R.layout.layout_item_storage_list2);
        rv.setAdapter(adapter);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            StorageBean.ResultBean resultBean = (StorageBean.ResultBean) getIntent().getSerializableExtra("storebean");
            ArrayList<StorageBean.ResultBean> list = new ArrayList<>();
            list.add(resultBean);
            adapter.setListSize(list.size());
            adapter.setNewData(list);
            setBottomData();
        }

    }

    /**
     * 底部数据
     */
    private void setBottomData() {
        if (adapter.getData().size() > 0) {
            number.setVisibility(View.VISIBLE);
            number.setText("(" + adapter.getData().size() + ")");
        } else {
            number.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.rl_storage_add_list:
                doAddStorageList();
                break;
        }
    }

    /**
     * 加入入库列表list
     */
    private void doAddStorageList() {
        List<AddStorageBean> goodsList = adapter.getGoodsList();
        HttpParams add = ParamsUtils.getStorageaddParams();
        add.put("goods",JSONObject.toJSONString(goodsList));

        OkGo.<String>post(Constans.BASEURL).tag("storageadd").params(add).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                ResultBean resultBean = JSONObject.parseObject(response.body(), ResultBean.class);
                showToast(mContext, resultBean.getErrmsg());
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                showToast(mContext, "网络错误");
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
    }
}
